package com.amap.map2d.demo.util;

import android.content.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by Jason on 2016/4/17.
 */
public class Point {
    private double[] value;
    private boolean visited;
    private int cluster;


    public Point() {
    }

    public Point(double[] value, boolean visited, int cluster) {
        this.value = value;
        this.visited = visited;
        this.cluster = cluster;
    }


    public double[] getValue() {
        return value;
    }

    public void setValue(double[] value) {
        this.value = value;
    }

    public boolean isVisited() {
        return visited;
    }


    public void setVisited(boolean visited) {
        this.visited = visited;
    }

    public int getCluster() {
        return cluster;
    }

    public void setCluster(int cluster) {
        this.cluster = cluster;
    }

    @Override
    public String toString() {
        return value[0] + "," + value[1] + "的类别：" + cluster;
    }

    public static void initpointset(Set<Point> pointSet, Context context) throws Exception {

        List<double[]> list = new ArrayList<>();

        List<String> c = SelectUtil.query(context, "c");
        for (String temp : c) {
            String[] split = temp.split(",");
            double a = Integer.parseInt(split[1]) / 1000000.0;
            a = formatDouble1(a);
            double b = Integer.parseInt(split[0]) / 1000000.0;
            b = formatDouble1(b);
            double[] aa = {a, b};
            list.add(aa);
        }


        for (double[] point : list) {

            pointSet.add(new Point(point, false, 0));
        }
    }

    public static double formatDouble1(double d) {
        return (double) Math.round(d * 1000) / 1000;
    }


    public static double distance(Point p1, Point p2) {
        double[] p1a = p1.getValue();
        double[] p2a = p2.getValue();
        double distance = 0.0;

        for (int i = 0; i < p1a.length; i++) {
            distance += Math.pow(p1a[i] - p2a[i], 2);
        }
        distance = Math.sqrt(distance);
        return distance;
    }
}